package top.birdhk.TestAPI.window;


import org.apache.flink.streaming.api.TimeCharacteristic;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.timestamps.AscendingTimestampExtractor;
import org.apache.flink.streaming.api.functions.timestamps.BoundedOutOfOrdernessTimestampExtractor;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.util.OutputTag;
import top.birdhk.TestAPI.beans.SensorReading;


/**
 * 时间语义设置
 */
public class WindowTest_EventTime {

    public static void main(String[] args) throws Exception {


        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(1);

        // 设置时间语义(事件时间)
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);


        //DataStreamSource<String> inputSream = env.readTextFile("E:\\flink\\wordcount\\src\\main\\resources\\sensor.txt");
        DataStreamSource<String> inputSream = env.socketTextStream("localhost", 7777);


        SingleOutputStreamOperator<SensorReading> dataStream = inputSream.map(line -> {
            String[] split = line.split(",");
            return new SensorReading(split[0], new Long(split[1]), new Double(split[2]));
        })
                /** 正常升序的数据
                .assignTimestampsAndWatermarks(new AscendingTimestampExtractor<SensorReading>() {
                    @Override
                    public long extractAscendingTimestamp(SensorReading sensorReading) {
                        return 0;
                    }
                })
                **/
                /**
                 * 乱序数据设置时间戳和watermark
                 */
                .assignTimestampsAndWatermarks(new BoundedOutOfOrdernessTimestampExtractor<SensorReading>(Time.seconds(2)) {
                    // 提取时间戳，需要毫秒数
                    @Override
                    public long extractTimestamp(SensorReading sensorReading) {
                        return sensorReading.getTimestamp() * 1000L;
                    }
                });

        OutputTag<SensorReading> late = new OutputTag<>("late");


        SingleOutputStreamOperator<SensorReading> minTempStream = dataStream.keyBy("id")
                .timeWindow(Time.seconds(15))
                .allowedLateness(Time.minutes(1))
                .sideOutputLateData(late)
                .min("temperature");

        minTempStream.print("minTemp");

        minTempStream.getSideOutput(late).print("late");

        env.execute();

    }
}
